package com.chapter.sample.juc.locks;

import ch.qos.logback.core.util.TimeUtil;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 中断协商机制
 */
public class InterruptDemo {
    static volatile boolean isStop = false;

    static AtomicBoolean atomicBoolean = new AtomicBoolean(false);

    public static void main(String[] args) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "----" + Thread.interrupted());
        System.out.println(Thread.currentThread().getName() + "----" + Thread.interrupted());
        System.out.println("11111");
        Thread.currentThread().interrupt();
        System.out.println("22222");
        System.out.println(Thread.currentThread().getName() + "----" + Thread.interrupted());
        System.out.println(Thread.currentThread().getName() + "----" + Thread.interrupted());
    }

    private static void interruptedx() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("isInterrupted 线程结束");
                    System.out.println("isInterrupted over " + Thread.currentThread().isInterrupted());
                    break;
                }
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    //线程中断标志位，复位位false，停不下来，需要再次调用interupt设置状态true；让程序停止
                    System.out.println("isInterrupted 拯救开始 " + Thread.currentThread().isInterrupted());
                    Thread.currentThread().interrupt();
                    System.out.println("isInterrupted 拯救结束 " + Thread.currentThread().isInterrupted());
                    e.printStackTrace();
                }

                System.out.println("isInterrupted 0 3 " + Thread.currentThread().isInterrupted());
            }
        }, "t1");
        t1.start();

        TimeUnit.SECONDS.sleep(3);
        System.out.println("isInterrupted 0 0 " + t1.isInterrupted());

        new Thread(() -> {
            t1.interrupt();
            System.out.println("isInterrupted 0 1 " + t1.isInterrupted());
        }, "t2").start();
    }

    private static void interrupted() throws InterruptedException {
        //中断位true后，并不是立刻stop程序
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 300; i++) {
                System.out.println("-----------i : " + i);
            }
            System.out.println("t1.intrrupt()调用之后02:" + Thread.currentThread().isInterrupted());
        }, "t1");
        t1.start();
        System.out.println("t1.intrrupt()调用之前，t1线程的中断标识默认值：" + t1.isInterrupted());
        TimeUnit.MILLISECONDS.sleep(1);
        t1.interrupt();
        System.out.println("t1.intrrupt()调用之后01:" + t1.isInterrupted());

        TimeUnit.MILLISECONDS.sleep(3000);
        System.out.println("t1.intrrupt()调用之后03:" + t1.isInterrupted());
    }

    private static void interrupt() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("isInterrupted ：" + Thread.currentThread().isInterrupted());
                    break;
                }
                System.out.println("Interrupt");
            }
        }, "a");
        t1.start();

        TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {
            t1.interrupt();
        }, "b").start();
    }

    private static void atomicBooleanIntrrupt() throws InterruptedException {
        new Thread(() -> {
            while (true) {
                if (atomicBoolean.get()) {
                    System.out.println("isStop ：" + isStop);
                    break;
                }
                System.out.println("isStop");
            }
        }, "a").start();

        TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {
            atomicBoolean.set(true);
        }, "b").start();
    }

    private static void volatileInterrupt() throws InterruptedException {
        new Thread(() -> {
            while (true) {
                if (isStop) {
                    System.out.println("isStop ：" + isStop);
                    break;
                }
                System.out.println("isStop");
            }
        }, "a").start();

        TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {
            isStop = true;
        }, "b").start();
    }
}
